<?php
namespace App\Services\PingAnPay;

// use App\Services\PingAnPay\lib\pingan;
use App\Services\PingAnPay\lib\pingan_quick;

/**
* 已开通银行卡列表查询接口，调用该接口可以查询某商户下的某个会员名下开通的所有银行卡信息
* @author   Leen
* @date     2016/11/25
*/

// const SEND_URL = 'http://testebank.sdb.com.cn/khpayment/UnionAPI_Opened.do';
const SEND_URL = 'https://testebank.sdb.com.cn/khpayment/UnionAPI_Opened.do';
//如果手机端出现证书过期，可以使用http://testebank.orangebank.com.cn/khpayment/UnionAPI_Opened.do

class UnionApiOpened
{

    function unionApiQuery($customerId)
    {

        // $pingan = new pingan();
        $pingan = new pingan_quick();

        $data = array (
                // 'masterId' => $_POST['masterId'],
                'masterId' => config('pinganpay.masterId'),
                // 'customerId' => $_POST['customerId'],
                'customerId' => $customerId,
                // 'accNo' => $_POST['accNo'],
                //'customerId' => '909090',
        );

        $xml_data = $pingan->array_to_xml ( $data );
        // $merchantCertFile = '../merchant/2000311146.pfx';
        $merchantCertFile = config('pinganpay.merchant_cert_file');

        // 获取签名后的orig和sign
        $orig = $pingan->getOrig ( $xml_data );
        $sign = $pingan->getSign ( $merchantCertFile, $xml_data );
/*echo '<BR>签名后的orig:<BR>'.$orig;
echo '<BR><BR>签名后的sign:<BR>'.$sign;*/

        // 通过curl请求接口
        $parms = 'orig=' . $orig . '&sign=' . $sign;
        $rsponse = $pingan->curl ( SEND_URL, $parms );

        // 通过字符串截取获取orig
        $rsponseData = explode ( 'orig=', $rsponse );
        $rsponse2Data = explode ( 'SDBPAYGATE=', $rsponseData [1] );
/*echo '<BR><BR>字符串截取获取的rsponseData:<BR>'; print_r($rsponseData);
echo '<BR><BR>字符串截取获取的rsponse2Data:<BR>'; print_r($rsponse2Data);*/

        $rsponseSign = explode ( 'sign=', $rsponse );
        // $rsponse2Sign = explode ( 'orig=', $rsponseSign [0] );
        $rsponse2Sign = explode ( 'orig=', $rsponseSign [1] );
/*echo '<BR><BR>rsponseSign:<BR>'; print_r($rsponseSign);
echo '<BR><BR>rsponse2Sign:<BR>'; print_r($rsponse2Sign);*/

        // 解码
        $formOrig = $pingan->_base64_url_decode ( $rsponse2Data [0] );
        $formSign = $pingan->_base64_url_decode ( $rsponse2Sign [0] );
/*echo '<BR><BR>解码后的formOrig:<BR>'.$formOrig;
echo '<BR><BR>解码后的formSign:<BR>'.$formSign;*/

        // $tTrustPayCertFile = 'merchant/paygate.cer';
        $tTrustPayCertFile = config('pinganpay.t_trust_pay_cert_file');

        //2、验证签名是否正确
        $result = $pingan->validate($formOrig, $formSign, $tTrustPayCertFile);
        if ($result) {
            // echo 'success';
        } else {
            // exit('error');

            return false;
        }

        // $xml_dom = str_replace("GBK", "utf-8", $formOrig);
        // $result = $pingan->xml_to_array($xml_dom);
        // 输出结果
        $formOrig = iconv("GBK", "UTF-8", $formOrig);
        $orig_data = $pingan->xml_to_array($formOrig);

        /**
         *  ["masterId"]=> string(10) "unionpay01"
         *  ["customerId"]=> string(4) "0001"
         *  ["plantBankId"]=> string(5) "PAB01"
         *  ["status"]=> string(1) "1"
         *  ["date"]=> string(14) "20160527024828"
         *   ["telephone"]=> string(11) "135****5506"
         *   ["accNo"]=> string(4) "0018"
         *   ["OpenId"]=> string(26) "unionpay012016052743419513"
         *    ["errorCode"]=> string(0) ""
         *    ["errorMsg"]=> string(0) ""
         *    ["bankType"]=> string(2) "01"
         *    ["plantBankName"]=> string(12) "平安银行" }
         *
         *    */
        //var_dump($orig_data);
        // echo json_encode($orig_data);
        return json_encode($orig_data);
    }

}


?>